package com.example.easyscript.service.master;

import com.example.easyscript.bean.task.*;
import com.example.easyscript.dto.Req.AddTaskReq;
import com.example.easyscript.dto.Resp.ManagerInfoReq;
import com.example.easyscript.dto.Resp.RunningTaskInfo;
import com.example.easyscript.service.master.intf.TaskManagerService;
import com.example.easyscript.service.task.EntranceService;
import com.example.easyscript.service.task.OperationService;
import com.example.easyscript.service.task.StatusService;
import com.example.easyscript.service.task.TaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 任务管理器
 */
@Service
public class TaskManagerServiceImpl implements TaskManagerService {

    @Resource
    private TaskService taskService;

    @Resource
    private StatusService stateService;

    @Resource
    private EntranceService entranceService;

    @Resource
    private OperationService operationService;

    //任务集
    private final Map<Integer, TaskBean> taskBeanMap;

    //任务队列
    private final LinkedList<Integer> taskQue;

    public TaskManagerServiceImpl() {
        taskBeanMap = new HashMap<>();
        taskQue = new LinkedList<>();
    }

    @Override
    public void initManager() {
        //todo 初始化任務管理器
    }

    @Override
    public void cleanAll() {
        taskBeanMap.clear();
        taskQue.clear();
    }

    @Override
    public void addTask(Integer taskId) {
        AddTaskReq addTaskReq = new AddTaskReq();
        addTaskReq.setTaskId(taskId);
        addTaskReq.setOperationList(Collections.EMPTY_LIST);
        addTask(addTaskReq);
    }

    @Override
    public void addTask(AddTaskReq addTaskReq) {
        Integer taskId = addTaskReq.getTaskId();

        if (taskBeanMap.containsKey(taskId)) {
            //抛异常该任务已经有了！
            throw new RuntimeException("该任务已经包含在内");
        }

        TaskBean taskBean = loadTaskById(addTaskReq);
        taskBeanMap.put(taskBean.getId(), taskBean);
        taskQue.add(taskBean.getId());
    }

    @Override
    public TaskBean getNextTask() throws Exception {
        if (taskQue.size() == 0) {
            throw new RuntimeException("任务队列为空");
        }

        Integer taskId ;
        TaskBean taskBean ;
        int time = 0 ;
        while(time<taskQue.size()){
            taskId = taskQue.getFirst();
            taskBean = taskBeanMap.get(taskId);
            if(taskBean.canRun()){
                taskBean.resetWaitTime();
                return taskBean;
            }
            taskQue.pop();
            taskQue.push(taskId);
            time++;
        }
        throw new Exception("无任务可运行");
    }


    private TaskBean loadTaskById(AddTaskReq addTaskReq) {
        Integer taskId = addTaskReq.getTaskId();
        List<Integer> operationList = addTaskReq.getOperationList();
        TaskBean taskBean = taskService.getTaskBeanById(taskId);
        Map<Integer, StatusBean> stateBeanMap = stateService.getStateBeanMapByTaskId(taskId);
        Map<Integer, OperationBean> operationBeanMap = operationService.getOperationBeanMapByTaskId(taskId);
        Map<Integer, EntranceBean> entranceBeanMap = entranceService.getEntranceBeanMapByTaskId(taskId);
        StateDiagram stateDiagram = new StateDiagram(stateBeanMap, entranceBeanMap);
        taskBean.init(stateDiagram, operationBeanMap, operationList);
        return taskBean;
    }


    @Override
    public void deleteTask(Integer taskId) {

    }

    @Override
    public void stopTask(Integer taskId) {

    }

    @Override
    public List<RunningTaskInfo> getManagerInfo() {
        List<RunningTaskInfo> infos = new ArrayList<>();
        for (TaskBean value : taskBeanMap.values()) {
            RunningTaskInfo runningTaskInfo = new RunningTaskInfo(value);
            infos.add(runningTaskInfo);
        }
        return infos;
    }

    @Override
    public void addRunnerOperation(Integer taskId, Integer operationId) {
        taskBeanMap.get(taskId).addRunOperation(operationId);
    }

}
